#include "CacheRecv.h"

CacheRecv::CacheRecv(QObject *parent):CacheBase(parent)
{

}

void CacheRecv::recvUploadInfo(const QString &info)
{
    QList<QString> infoList;
    infoList << info;
    saveUploadInfo(infoList);
}
void CacheRecv::recvCommonInfo(const QString &info)
{
    QList<QString> infoList;
    infoList << info;
    saveCommonInfo(infoList);
}

void CacheRecv::recvTidInfo(const QString &info)
{
    QList<QString> infoList;
    infoList << info;
    saveTidInfo(infoList);
}

void CacheRecv::checkTidInfo(const QString &info, bool &existState)
{
    existState = checkTidExist(info);
//    QList<QVariant> infoList;
//    infoList << info;
//    QList<bool> existStateList = checkTidExist(infoList);
//    if(existStateList.size()>0)
//    {
//        existState = existStateList.at(0)
//    }
//    else
//    {
//        existState = false;
//    }
}

bool CacheRecv::saveUploadInfo(const QList<QString> &info)
{
    //	检查输入参数
    bool state = false;
    if (info.size()<=0)
    {
        state = false;
        return state;
    }
    QList<QString>  key;
    key << s_infoKey << s_stateKey;
    QList<QList<QVariant>> value;
    foreach(const QString &tempInfo,info)
    {
        QString deInfoValue;
        QString deStatusInfoValue;
        QList<QVariant> tempValue;
        QString enInfoValue;
        QString statusInfoValue;
        QString enStatusInfoValue;
        // 生成加密信息字符串
        encryptSimple(tempInfo,enInfoValue);
        // decryptSimple(enInfoValue,deInfoValue);
        //	生成加密状态信息
        QJsonObject tempObj;
        tempObj.insert(s_createTimeStampKey,QDateTime::currentDateTime().toMSecsSinceEpoch()/1000);
        tempObj.insert(s_uploadTimesKey,0);
        statusInfoValue = QString(QJsonDocument(tempObj).toJson(QJsonDocument::JsonFormat::Compact));
        encryptSimple(statusInfoValue, enStatusInfoValue);
        // decryptSimple(enStatusInfoValue,deStatusInfoValue);
        tempValue << enInfoValue << enStatusInfoValue;
        // tempValue << deInfoValue << deStatusInfoValue;
        value << tempValue;
    }
    state = m_dataBaseInterface->writeRow(s_uploadInfoTableName, key, value);
    return state;
}

bool CacheRecv::saveCommonInfo(const QList<QString> &info)
{
    //	检查输入参数
    bool state = false;
    if (info.size()<=0)
    {
        state = false;
        return state;
    }
    QList<QString>  key;
    key << s_idKey <<s_infoKey ;
    QList<QList<QVariant>> value;
    foreach(const QString &tempInfo,info)
    {
        QList<QVariant> tempValue;
        QString enInfoValue;
        // 生成加密信息字符串
        encryptSimple(tempInfo,enInfoValue);
        tempValue << QVariant(static_cast<qlonglong>(s_commonTablePrimaryKey)) <<enInfoValue;
        value << tempValue;
    }
    qDebug()<<"CacheRecv:common:"<<"key="<<key<<" value="<<value;
    state = m_dataBaseInterface->replaceRow(s_commonInfoTableName, key, value);
    return state;
}

bool CacheRecv::saveTidInfo(const QList<QString> &info)
{
    bool state = false;
    if (info.size()<=0)
    {
        state = false;
        return state;
    }
    QList<QString>  key;
    key << s_infoKey ;
    QList<QList<QVariant>> value;
    foreach(const QString &tempInfo,info)
    {
        QList<QVariant> tempValue;
        QString enInfoValue;
        // 生成加密信息字符串
        encryptSimple(tempInfo,enInfoValue);
        //	生成加密状态信息
        tempValue << enInfoValue ;
        value << tempValue;
    }
    state = m_dataBaseInterface->replaceRow(s_tidInfoTableName, key, value);
    return state;

}

bool CacheRecv::checkTidExist(const QString &info)
{
    qlonglong rowNum;
    QString enInfo;
    encryptSimple(info,enInfo);
    rowNum = m_dataBaseInterface->existRowNum(s_tidInfoTableName, s_infoKey,enInfo);
    if(rowNum > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
